package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardServices;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * date: 2024/09/11 10:24<br/>
 *
 * @author: jmy <br/>
 */

@Slf4j
@Api(tags = "板块接口")
@RestController
@RequestMapping("/board")
public class BoardController {
    @Resource
    private IBoardServices boardServices;

    //在配置文件里面找，如果找不到，就默认 9
    @Value("${spring-forum.index.board-num:9}")
    private Integer indexBoardNum;

    /**
     * date: 2024/09/11 10:26<br/>
     * Description:
     * 查询首页板块
     *
     * @author: jmy <br/>
     */

    @ApiOperation("获取板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        log.info("board/topList 首页板块参数为:" + indexBoardNum);
        List<Board> boards = boardServices.selectByNum(indexBoardNum);

        if (boards == null) {
            return AppResult.failed();
        }
        return AppResult.success(boards);
    }

    /**
     * date: 2024/09/14 15:24<br/>
     * Description:通过板块id获取板块id
     *
     * @author: jmy <br/>
     */
    @ApiOperation("获取板块信息")
    @GetMapping("/getBoardById")
    public AppResult<Board> getBoardById(@ApiParam("板块id") @NotNull @RequestParam("boardId") Long boardId) {
        log.info("board/boardInfo 接收参数: boardId :" + boardId);
        if (boardId <= 0) {
            log.error(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        //查询板块
        Board board = boardServices.selectByPrimaryKey(boardId);
        //板块不存在
        if (board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
        }
        //成功
        return AppResult.success(board);
    }
    /**
     * date: 2024/09/16 11:00<br/>
     * Description: 更新板块中帖子数量 （数量减一）
     * @author: jmy <br/>
     */

}
